home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Documentation / Books / Learn Java on the Macintosh / Learn Java Projects / 11.05 - simple draw / SimpleDraw.java < prev    next >
Text File  |  1996-04-22  |  3KB  |  104 lines

  1. /* -------------------------------------------------------------
  2.  
  3. This applet paints a circle or square of the color you've chosen
  4. wherever you click. 
  5.  
  6. Java's classes: Applet    (applet)
  7.                 Event     (awt)     user-generated action
  8.                 Graphics  (awt)     used for drawing
  9.                 Color     (awt)     defines colors
  10.                 Choice    (awt)     shape and color selection choices
  11.  
  12. Custom classes: SimpleDraw
  13.                 Circle              defines and draws circles
  14.                 Square              defines and draws squares
  15.  
  16. ------------------------------------------------------------- */
  17.  
  18. import java.applet.Applet;
  19. import java.awt.*;
  20.  
  21. public class SimpleDraw extends Applet {
  22.    Shape   currentShape = null;
  23.    Choice  shapeChoice;
  24.    Choice  colorChoice;
  25.    
  26.    /** Create the GUI. */
  27.    public void init() {
  28.    
  29.       shapeChoice = new Choice();
  30.       shapeChoice.addItem("Circle");
  31.       shapeChoice.addItem("Square");
  32.       add(shapeChoice);
  33.       
  34.       colorChoice = new Choice();
  35.       colorChoice.addItem("Red");
  36.       colorChoice.addItem("Green");
  37.       colorChoice.addItem("Blue");
  38.       add(colorChoice);
  39.    }
  40.    
  41.    /** Draw the current shape. */
  42.    public void paint(Graphics g) {
  43.       if (currentShape != null)
  44.          currentShape.draw(g);
  45.    }
  46.    
  47.    /** Create a new shape. */
  48.    public boolean mouseUp(Event e, int x, int y) {
  49.       Color  color;
  50.       String shapeString = shapeChoice.getSelectedItem();
  51.       String colorString = colorChoice.getSelectedItem();
  52.       
  53.       if (colorString.equals("Red"))
  54.          color = Color.red;
  55.       else if (colorString.equals("Green"))
  56.          color = Color.green;
  57.       else
  58.          color = Color.blue;
  59.  
  60.      // Create a new shape of the appropriate type. Without inheritance,
  61.      // we have to write duplicate code for each of the shape types.
  62.      if (shapeString.equals("Circle"))
  63.          currentShape = new Circle();
  64.      else
  65.          currentShape = new Square();
  66.          
  67.       currentShape.color = color;
  68.       currentShape.x = x;
  69.       currentShape.y = y;
  70.       
  71.       repaint();
  72.       
  73.       return true;
  74.    }
  75.  
  76. }
  77.  
  78. /** Shapes provide common characteristics for the circle and square. */
  79. abstract class Shape {
  80.    static public final int shapeRadius = 20;
  81.    
  82.    Color color;
  83.    int   x;
  84.    int   y;
  85.    
  86.    abstract void draw(Graphics g);
  87. }
  88.  
  89. /** Draws and maintains circle information. */
  90. class Circle extends Shape {
  91.    void draw(Graphics g) {
  92.       g.setColor(this.color);
  93.       g.fillOval(this.x - shapeRadius, this.y - shapeRadius, shapeRadius * 2, shapeRadius * 2);
  94.    }
  95. }
  96.  
  97. /** Draws and maintains square information. */
  98. class Square extends Shape{
  99.    void draw(Graphics g) {
  100.       g.setColor(this.color);
  101.       g.fillRect(this.x - shapeRadius, this.y - shapeRadius, shapeRadius * 2, shapeRadius * 2);  
  102.    }
  103. }
  104.